#include "stdafx.h"
#include "EmptyWindow.h"

using namespace OIS;

EmptyWindow::EmptyWindow(u32 id, const Vector2D<float>& position, float scale)
{
    Init(id, position, Vector2D<float>(), scale);
}

EmptyWindow::EmptyWindow(u32 id, const Vector2D<float>& position, const Vector2D<float>& size)
{
    Init(id, position, size);
}

void EmptyWindow::Init(u32 id, const Vector2D<float>& position, const Vector2D<float>& size, float scale)
{
    m_fDragging = false;
    m_pResourceManager = NULL;
    m_pSpriteWindow = NULL;
    m_pSpriteFrame = NULL;
    m_quitGraphic = NULL;

    Vector2D<float> newSize = size;
    m_pResourceManager = Utilities::GetResourceManager("Menus");

    if (m_pResourceManager)
    {
        HTEXTURE windowTex = m_pResourceManager->GetTexture("EmptyWindow");
        HTEXTURE frameTex = m_pResourceManager->GetTexture("Frame");
        HTEXTURE quitTex = m_pResourceManager->GetTexture("X");

        if (windowTex && frameTex)
        {
            m_pSpriteWindow = new hgeSprite(windowTex);
            m_pSpriteFrame = new hgeSprite(frameTex);
            m_quitGraphic = new hgeSprite(quitTex);

            HGE* pHge = hgeCreate(HGE_VERSION);

            float width = pHge->Texture_GetWidth(windowTex) * scale;
            float height = pHge->Texture_GetHeight(windowTex) * scale;

            if (size.Magnitude() == 0)
            {
                newSize = Vector2D<float>(width, height);
                m_scale.Set(scale, scale);
            }
            else
            {
                m_scale.Set(size.GetX() / width, size.GetY() / height);
            }
        }
    }
  
    GUIObject::Init(id, position, newSize);
}

void EmptyWindow::OnMouseButtonDown(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
    if (arg.state.buttonDown(id) && MouseButtonID::MB_Left == id)
    {
        float mouseX = arg.state.X.abs;
        float mouseY = arg.state.Y.abs;
        m_fDragging = true;
        m_startMousePositionDrag = Vector2D<float>(mouseX, mouseY);
    }
}

void EmptyWindow::OnMouseButtonUp(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
    m_fDragging = false;
}

void EmptyWindow::OnMouseOver()
{
}

void EmptyWindow::Render()
{
    /*
    if (m_pSpriteFrame)
    {
        m_pSpriteFrame->RenderEx(GetPosition().GetX() - 5, GetPosition().GetY() - 10, 0, m_scale.GetX() * (GetSize().GetX() + 10) / GetSize().GetX(), m_scale.GetY() * (GetSize().GetY() + 15) / GetSize().GetY());
    }

    if (m_quitGraphic)
    {
        m_quitGraphic->RenderEx(GetPosition().GetX() + GetSize().GetX() - 6, GetPosition().GetY() - 10, 0, 1, 1);
    }
    */
    if (m_pSpriteWindow)
    {
        m_pSpriteWindow->RenderEx(GetPosition().GetX(), GetPosition().GetY(), 0, m_scale.GetX(), m_scale.GetY());
    }
}


bool EmptyWindow::Update()
{   
    if (m_fDragging)
    {
        float mouseX, mouseY;
        HGE* pHGE = hgeCreate(HGE_VERSION);
        pHGE->Input_GetMousePos(&mouseX, &mouseY);   //get the current mouse position

        Vector2D<float> offset = Vector2D<float>(mouseX, mouseY) - m_startMousePositionDrag;
        m_startMousePositionDrag = Vector2D<float>(mouseX, mouseY);
        SetPosition(GetPosition() + offset);
    }
    
    return false;
}

void EmptyWindow::Destroy()
{
    Utilities::SafeDelete<hgeSprite>(m_pSpriteWindow);
    Utilities::SafeDelete<hgeSprite>(m_pSpriteFrame);
    Utilities::SafeDelete<hgeSprite>(m_quitGraphic);
}